package com.bci.pwtz.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.Zydl;
import com.bci.pwtz.mysql.model.Zydljdlref;
import com.bci.pwtz.service.ZydlService;
import com.bci.pwtz.service.ZydljdlrefService;

@Controller
@RequestMapping("/user/zydljdlref")
public class ZydljdlrefController extends AbstractController
{
@Autowired
private ZydljdlrefService service;
@Autowired
private ZydlService zydlService;

@RequestMapping(value="/list",method=RequestMethod.POST)
@ResponseBody
public PaginationList<Zydljdlref> list(String deviceid,int pageSize,int pageNo) throws PwtzDatabaseException{
    PaginationList<Zydljdlref> plist=new PaginationList<Zydljdlref>();
    try{
        plist=service.list(deviceid, PwtzConstants.PORTAL_TYPE_WEB, null, null, pageSize, pageNo);
    }catch(PwtzDatabaseException e){
        e.printStackTrace();
    }
    return plist;
}

@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map<String,String> add(String deviceid,String linecode,String embeddingunitnumber,String connectioncable){
    Map<String, String> map = new HashMap<String, String>();
    try
    {
//        String[] list = decodeURL(data.split("&"));
//        for (int i = 0; i < list.length; i++)
//        {
//            String[] array = list[i].split("=");
//            if (array.length == 1)
//            {
//                map.put(array[0], "");
//            }
//            else
//            {
//                map.put(array[0], array[1]);
//            }
//        }
        Zydljdlref device=new Zydljdlref();
        device.setCablepitid(deviceid);
        device.setLinecode(linecode);
        device.setConnectioncable(connectioncable);
        device.setEmbeddingunitnumber(embeddingunitnumber);
        UUID uuid=UUID.randomUUID();
        device.setId(uuid.toString());
        boolean flag=service.isRefExist(deviceid, connectioncable);
        if(flag){
            device=service.add(device);
            if (device !=null)
            {
                setLog(device.getId(),getLastCompanyId(), AbstractController.中压电缆井关联, AbstractController.add, "添加中压电缆井关联成功", getUserId());
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null, "添加中压电缆井关联成功");
                map.put("deviceid", uuid.toString());
            }
            else
            {
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "添加中压电缆井关联失败");
            }
        }else{
            Zydl zydl=zydlService.load(connectioncable);
            String dlName="";
            if(zydl!=null){
                dlName=zydl.getDevicename();
            }
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null, "该电缆井与"+dlName+"已经存在关联关系");
        }
    }
    catch (PwtzDatabaseException e)
    {
        e.printStackTrace();
    }
    return map;
}

@RequestMapping(value="/edite",method=RequestMethod.POST)
@ResponseBody
public Map<String,String> edite(String data){
    Map<String, String> map = new HashMap<String, String>();
    try
    {
        String[] list = decodeURL(data.split("&"));
        for (int i = 0; i < list.length; i++)
        {
            String[] array = list[i].split("=");
            if (array.length == 1)
            {
                map.put(array[0], "");
            }
            else
            {
                map.put(array[0], array[1]);
            }
        }
        Zydljdlref device=new Zydljdlref();
        device.setCablepitid(map.get("cablepitid"));
        device.setConnectioncable(map.get("connectioncable"));
        device.setEmbeddingunitnumber(map.get("embeddingunitnumber"));
        device.setId(map.get("id"));
        boolean flag=service.isRefExist(map.get("cablepitid"), map.get("connectioncable"));
        if(flag){
            int result=service.update(device);
            if (result>0)
            {
                setLog(device.getId(),getLastCompanyId(), AbstractController.中压电缆井关联, AbstractController.update, "修改中压电缆井关联成功", getUserId());
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null, "修改中压电缆井关联成功");
            }
            else
            {
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "修改中压电缆井关联失败");
            }
        }else{
            Zydl zydl=zydlService.load(map.get("connectioncable"));
            String dlName="";
            if(zydl!=null){
                dlName=zydl.getDevicename();
            }
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null, "该电缆井与"+dlName+"已经存在关联关系");
        }
    }
    catch (UnsupportedEncodingException e)
    {
        e.printStackTrace();
    }
    catch (PwtzDatabaseException e)
    {
        e.printStackTrace();
    }
    return map;
}
@RequestMapping(value="delete",method=RequestMethod.POST)
@ResponseBody
public Map<String,String> delete(String deviceid){
    Map<String, String> map = new HashMap<String, String>();
    try
    {
        int result=service.delete(deviceid);
        if (result>0)
        {
            setLog(deviceid,getLastCompanyId(), AbstractController.中压电缆井关联, AbstractController.delete, "删除中压电缆井关联成功", getUserId());
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null, "删除中压电缆井关联成功");
        }
        else
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "删除中压电缆井关联失败");
        }
    }
    catch (PwtzDatabaseException e)
    {
        e.printStackTrace();
    }
    catch (CanNotDeleteException e)
    {
        e.printStackTrace();
    }
    return map;
}
}
